package util;

public class Timer 
{

    public static final float NANO_PER_SEC = 1E9f;
    public static final float SEC_PER_NANO = 1.0f / 1E9f;
    private long time;

    public Timer() 
    {
        this(false);
    }

    public Timer(boolean startNow) 
    {
        if (startNow) 
        {
            time = getSystemTime();
        } 
        else 
        {
            time = 0L;
        }
    }

    public boolean isRunning() 
    {
        return (time != 0L);
    }

    public void start() 
    {
        time = getSystemTime();
    }

    public void resetTimeAndContinue() 
    {
        if (isRunning()) 
        {
            time = getSystemTime();
        }
    }

    public float getTimePassed() 
    {
        if (isRunning()) 
        {
            return (getSystemTime() - time) * SEC_PER_NANO;
        } 
        else 
        {
            return 0;
        }
    }

    public void moveBackBy(float timeSec) 
    {
        long deltaTime = (long) (NANO_PER_SEC * timeSec + 0.5f);
        time += deltaTime;
    }

    public void stop() 
    {
        time = 0L;
    }

    public float stopAndReturnTime() 
    {
        float result = getTimePassed();
        stop();
        return result;
    }

    public static long getSystemTime() 
    {
        return System.nanoTime();
    }

}
